package study.saas.web.portal.sys.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.order.entity.Commodity;
import study.saas.order.service.CommodityFacade;
import study.saas.order.service.LotteryFacade;
import study.saas.order.service.model.CommodityFiterDTO;
import study.saas.order.service.model.CommodityLotteryDTO;
import study.saas.order.service.vo.LotteryPerson;

import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author ctc
 */
@Api(tags = "抽奖管理")
@RestController
@RequestMapping(value = "sys/lottery")
@Slf4j
public class LotteryController {
    /**
     * 1、设置抽奖奖品
     * 2、查询参与抽奖人数
     * 3、抽奖
     */
    @Reference(version = "1.0.0")
    private LotteryFacade lotteryFacade;
    @Reference(version = "1.0.0")
    private CommodityFacade commodityFacade;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "查询参与抽奖商品")
    @RequestMapping(value = "/getCommodity", method = RequestMethod.GET)
    public ApiResultV2 getCommodity() {
        List<CommodityLotteryDTO> data = lotteryFacade.getCommodity();

        return ApiResultV2.ok(data);
    }

    @ApiOperation(value = "抽奖")
    @RequestMapping(value = "/lottery", method = RequestMethod.POST)
    public ApiResultV2 lottery() {
        List<CommodityLotteryDTO> data = lotteryFacade.getCommodity();
        int x = (int)(Math.random() * data.size());
        log.error("奖品为{}：{}",x,data.get(x));
        return ApiResultV2.ok(data.get(x));
    }


    @ApiOperation(value = "参与抽奖")
    @RequestMapping(value = "/participation", method = RequestMethod.GET)
    public ApiResultV2 participation(String name) {
        String key = "lottery-"+LocalDate.now();
        Long count = redisTemplate.opsForSet().add(key, name);
        redisTemplate.expire(key, 1, TimeUnit.HOURS);

        return ApiResultV2.ok(count);
    }

    @ApiOperation(value = "查看参加抽奖人")
    @RequestMapping(value = "/getLotteryPerson", method = RequestMethod.GET)
    public ApiResultV2 getLotteryPerson() {
        String key = "lottery-"+LocalDate.now();
        Set<String> nameList = redisTemplate.opsForSet().members(key);

        List<LotteryPerson> collect = nameList.stream().map(LotteryPerson::new).collect(Collectors.toList());
        return ApiResultV2.ok(collect);
    }

    @ApiOperation(value = "开始抽奖")
    @RequestMapping(value = "/StartLottery", method = RequestMethod.GET)
    public ApiResultV2 StartLottery(int count) {
        String key = "lottery-"+LocalDate.now();
        List<String> nameList = new ArrayList<>();
//        List<String> nameList = redisTemplate.opsForSet().randomMembers(key, count);
        while (count-->0&&redisTemplate.opsForSet().size(key)!=0){
            nameList.add((String) redisTemplate.opsForSet().pop(key));
        }
        return ApiResultV2.ok(nameList);
    }


}
